import getUUID from '../utils/getUUID.js'
import { lazyReportCache } from '../utils/report.js'
import { getPagePath, getSelector } from '../utils/common.js'

export default function onOperate() {
    ['mousedown', 'touchstart'].forEach(eventType => {
        window.addEventListener(eventType, event => {
            requestIdleCallback(() => {
                const target = event.target
                const { top, left } = target.getBoundingClientRect()
                const data = {
                    type: 'behavior',
                    subType: 'operate',
                    startTime: event.timeStamp,
                    selector: getSelector(target), // 最后一个操作的元素
                    pagePath: getPagePath(),
                    eventType,
                    top,
                    left,
                    pageHeight: document.documentElement.scrollHeight || document.body.scrollHeight,
                    scrollTop: document.documentElement.scrollTop || document.body.scrollTop,
                    target: target.tagName,
                    outerHTML: target.outerHTML,
                    innerHTML: target.innerHTML,
                    width: target.offsetWidth,
                    height: target.offsetHeight,
                    viewport: {
                        width: window.innerWidth,
                        height: window.innerHeight,
                    },
                    uuid: getUUID(),
                }
                lazyReportCache(data)
            })
        })
    })
}
